package com.sr.examination;

import android.app.Activity;
import android.content.Context;
import android.content.DialogInterface;
import android.view.View;

import com.sr.examination.main.MainActivity;
import com.sr.examination.other.WelcomeActivity;
import com.sr.examination.user.Account;
import com.tl.commonlibrary.AppConfig;
import com.tl.commonlibrary.ui.ActivityStack;
import com.tl.commonlibrary.ui.widget.CustomAlertDialog;
import com.tl.libmanager.MainAppInterface;

/**
 * @author JongSung
 * @Description app module的入口,跨module调用需要经过此类中的方法
 * @date 2017/6/14 0022
 */

public class LibEntrance implements MainAppInterface {
    private boolean isShowingLoginDialog = false;
    private CustomAlertDialog logOffDialog;

    @Override
    public void tokenInvalid() {
        tokenInvalid(false);
    }

    @Override
    public void tokenInvalid(final boolean needFinish) {
        if (logOffDialog != null && logOffDialog.isShowing()) {
            return;
        }
        if (Account.isNotLogged()) {
            return;
        }
        final String userName = Account.getMobile();
        Account.logOff();
        final Activity activity = ActivityStack.get().currentBaseFragmentActivity();
        if (activity != null) {
            isShowingLoginDialog = true;
            activity.runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    logOffDialog = new CustomAlertDialog(activity);
                    logOffDialog.setLeftText("取消");
                    logOffDialog.setRightText("确定");
                    logOffDialog.setContent(activity.getResources().getString(R.string.token_invalid));
                    logOffDialog.setOnDismissListener(new DialogInterface.OnDismissListener() {
                        @Override
                        public void onDismiss(DialogInterface dialog) {
                            isShowingLoginDialog = false;
                            logOffDialog = null;
                        }
                    });
                    logOffDialog.setListener(new CustomAlertDialog.OnAlertClickListener() {
                        @Override
                        public void onLeftClicked(View view) {
                            if (needFinish && !(activity instanceof MainActivity)) {
                                activity.finish();
                            }
                            isShowingLoginDialog = false;
                            logOffDialog = null;
                        }

                        @Override
                        public void onRightClicked(View view) {
                            Account.forceLogin(activity, userName);
                            if (needFinish && !(activity instanceof MainActivity)) {
                                activity.finish();
                            }
                            isShowingLoginDialog = false;
                            logOffDialog = null;
                        }
                    });
                    logOffDialog.show();
                }
            });
        }
    }

    @Override
    public void forceLogin(Context context) {
        Account.forceLogin(context);
    }

    @Override
    public void forceLogin(Context context, String loginName) {
        Account.forceLogin(context, loginName);
    }

    @Override
    public void login(Context context) {
        Account.forceLogin(context, Account.getMobile());
    }


    @Override
    public void startApp(Context context) {
        WelcomeActivity.start(context);
    }

    @Override
    public boolean checkLogin(Context context) {
        return Account.isLogged(context);
    }


    @Override
    public void onActivityPush(Activity activity) {
        ActivityStack.get().push(activity);
    }

    @Override
    public void onActivityPop(Activity activity) {
        ActivityStack.get().pop(activity);
    }

    @Override
    public void onActivityStart(Activity activity) {
        ActivityStack.get().onStarted(activity);
    }

    @Override
    public void onActivityStop(Activity activity) {
        ActivityStack.get().onStop(activity);
    }

    @Override
    public boolean debuggable() {
        return AppConfig.DEBUG();
    }

}
